﻿using bpms.Models;
using bpms.Service;
using Microsoft.AspNetCore.Mvc;

namespace bpms.Controllers
{
    /// <summary>
    /// 管理员模块
    /// </summary>
    public class AdminUserController : BaseController
    {
        private readonly AdminUserService _adminUserService;
        public AdminUserController(AdminUserService adminUserService)
        {
            _adminUserService = adminUserService;
        }

        // 编辑
        public async Task<IActionResult> Edit(int id)
        {
            if (id == null)
            {
                return NotFound();
            }
            // 查询管理员用户
            var admin = _adminUserService.GetById(id);
            if (admin == null)
            {
                return NotFound();
            }

            return View(admin);
        }

        // POST: Admin/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Phone,Password")] AdminUser admin)
        {
            if (id != admin.Id)
            {
                TempData["ErrorMessage"] = "没找到用户信息";
                return NotFound();
            }
            //登录名不能修改，所以不验证
            ModelState.Remove("LoginName");
            if (ModelState.IsValid)
            {

                // 查询管理员用户
                var existadmin = _adminUserService.GetById(id);
                if (existadmin == null)
                {
                    TempData["ErrorMessage"] = "没找到用户信息";
                    return NotFound();
                }
                admin.Id = id;
                int i = _adminUserService.update(admin);
                if (i > 0)
                {
                    TempData["SuccessMessage"] = "修改成功";
                }
                else
                {
                    TempData["ErrorMessage"] = "修改失败";
                }
            }

            return View(admin);
        }
    }
}
